package com.yu1996.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;  //循环条件
        char c = ' ';  //用于接收输入的字符
        while (flag) {
            System.out.println("s:显示数列   a:入列   g:出列   h:显示头元素   e:退出");
            c = scanner.next().charAt(0);
            switch (c) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入需要添加的数据：");
                    arrayQueue.addQueue(scanner.nextInt());
                    break;
                case 'g':
                    try {
                        int queue = arrayQueue.getQueue();
                        System.out.printf("出列的数据是：%d\n", queue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int queue = arrayQueue.headQueue();
                        System.out.printf("队列的头是：%d\n", queue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    flag = false;
                    break;
                default:
                    System.out.println("输入不合法，请重新输入");
                    break;
            }
        }
        System.out.println("退出成功，再见~");
    }

}


//定义队列对象
class ArrayQueue {

    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    //创建构造方法
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        rear = -1;
        front = -1;
    }

    /***
     * @description 判断队列是否已满
     *
     * @param
     * @date 2019/7/7 20:08
     * @return boolean
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /***
     * @description 判断队列是否为空
     *
     * @param
     * @date 2019/7/7 20:08
     * @return boolean
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /***
     * @description 打印队列
     *
     * @param
     * @date 2019/7/7 20:08
     * @return void
     */
    public void showQueue() {
        //判断是否为空
        if (isEmpty()) {
            System.out.println("队列为空，请先添加数据!");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\t", i, arr[i]);
        }
        System.out.println();
    }


    /***
     * @description 入列
     *
     * @param val
     * @date 2019/7/7 20:07
     * @return void
     */
    public void addQueue(int val) {
        //判断队列是否已满
        if (isFull()) {
            System.out.println("队列已满无法添加~~");
            return;
        }
        arr[++rear] = val;
    }

    /***
     * @description 出列
     *
     * @param
     * @date 2019/7/7 20:07
     * @return int
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，无法出列~~");
        }
        front++;
        return arr[front];
    }


    /***
     * @description 显示头元素
     *
     * @date 2019/7/7 20:16
     * @return int
     */
    public int headQueue() {
        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空，无法取出~~");
        }
        return arr[front + 1];
    }

}
